package com.lijiajia.cloud.authserver.security.oauth.user;

import org.springframework.security.oauth2.core.oidc.AddressStandardClaim;
import org.springframework.security.oauth2.core.oidc.OidcUserInfo;

import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * Github 用户信息
 * 来自OAuth 2.0受保护资源UserInfo端点。
 * <p>
 * 基于 user-info-url 接口 填充 oidc 1.0 userInfo信息
 *
 * @author lijiajia
 * @since 2019-05-21
 */
public class GithubUserInfo extends OidcUserInfo {

    /**
     * Constructs a {@code OidcUserInfo} using the provided parameters.
     *
     * @param claims the claims about the authentication of the End-User
     */
    public GithubUserInfo(Map<String, Object> claims) {
        super(claims);
    }

    @Override
    public String getSubject() {
        return this.getClaimAsString("id");
    }

    @Override
    public String getFullName() {
        return null;
    }

    @Override
    public String getGivenName() {
        return null;
    }

    @Override
    public String getFamilyName() {
        return null;
    }

    @Override
    public String getMiddleName() {
        return null;
    }

    @Override
    public String getNickName() {
        return this.getClaimAsString("name");
    }

    @Override
    public String getPreferredUsername() {
        return this.getClaimAsString("login");
    }

    @Override
    public String getProfile() {
        return this.getClaimAsString("html_url");
    }

    @Override
    public String getPicture() {
        return this.getClaimAsString("avatar_url");
    }

    @Override
    public String getWebsite() {
        return null;
    }

    @Override
    public String getEmail() {
        return this.getClaimAsString("email");
    }

    @Override
    public Boolean getEmailVerified() {
        return true;
    }

    @Override
    public String getGender() {
        return null;
    }

    @Override
    public String getBirthdate() {
        return null;
    }

    @Override
    public String getZoneInfo() {
        return null;
    }

    @Override
    public String getLocale() {
        return null;
    }

    @Override
    public String getPhoneNumber() {
        return null;
    }

    @Override
    public Boolean getPhoneNumberVerified() {
        return true;
    }

    @Override
    public AddressStandardClaim getAddress() {
        return null;
    }

    @Override
    public Instant getUpdatedAt() {
        String instantCtx = this.getClaimAsString("updatedAt");
        if (instantCtx == null) return null;
        return Instant.from(DateTimeFormatter.ISO_INSTANT.parse(instantCtx));
    }
}
